#include "ThreadAffinity.h"
#ifdef WIN32
#include <QThread>
#include "ThreadAffinityWin.h"
#else
#include <pthread.h>
#include "ThreadAffinityLinux.h"
#endif

ThreadAffinity::ThreadAffinity()
{
#ifdef WIN32
    affinity = new ThreadAffinityWin();
#else
    affinity = new ThreadAffinityLinux();
#endif
}

ThreadAffinity::~ThreadAffinity()
{
    delete affinity;
}

/**
 * @brief ThreadAffinity::setAffinity
 * 设置线程亲和性，此函数必须放在线程函数内调用。
 * @param processorIndexs 处理器列表
 * @return 设置结果
 */
bool ThreadAffinity::setAffinity(const QSet<int> &processorIndexs)
{
#ifdef WIN32
    quint64 threadId = (quint64)QThread::currentThreadId();
#else
    quint64 threadId = pthread_self();
#endif
    return affinity->setAffinity(threadId, processorIndexs);
}
